Option Strict Off
Option Explicit On
Imports Microsoft.VisualBasic.PowerPacks
Namespace BMLERP
	Friend Class frmRAR500
		Inherits System.Windows.Forms.Form
		Dim NewTrans As Byte
		Dim oldARDOC As String
		Dim oldARMDC As String



		Private Sub CboARCCD_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles CboARCCD.ClickEvent
			Dim RecCCI As ADODB.Recordset
			If eventArgs.area = 2 Then
				RecCCI = DBconn.Execute("Select CCM.CMDSC,CCI.CICCR from CCI INNER JOIN CCM ON CCI.CICCD=CCM.CMCCD where CICCD='" & cboARCCD.BoundText & "' order by CIDTE DESC")
				If Not RecCCI.EOF Then
					txtARCER.Text = RecCCI.Fields("CICCR").Value
					txtCMDSC.Text = RecCCI.Fields("CMDSC").Value
				Else
					txtARCER.Text = CStr(0)
				End If
				RecCCI.Close()
			End If
		End Sub

		Private Sub CboARCCD_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CboARCCD.Leave
			Dim RecCCI As ADODB.Recordset
			RecCCI = DBconn.Execute("Select CCM.CMDSC,CCI.CICCR from CCI INNER JOIN CCM ON CCI.CICCD=CCM.CMCCD where CICCD='" & cboARCCD.BoundText & "' order by CIDTE DESC")
			If Not RecCCI.EOF Then
				txtARCER.Text = RecCCI.Fields("CICCR").Value
				txtCMDSC.Text = RecCCI.Fields("CMDSC").Value
			Else
				txtARCER.Text = CStr(0)
			End If
			RecCCI.Close()
		End Sub

		Private Sub cboARCNO_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboARCNO.ClickEvent
			Dim RecRCM As ADODB.Recordset
			If eventArgs.area = 2 Then
				RecRCM = DBconn.Execute("Select CMNME FROM RCM")
				If Not RecRCM.EOF Then
					txtCMNME.Text = RecRCM.Fields("CMNME").Value

				Else
					txtCMNME.Text = ""
				End If
				RecRCM.Close()
			End If

		End Sub

		Private Sub cboARCNO_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cboARCNO.Leave
			Dim RecRCM As ADODB.Recordset
			RecRCM = DBconn.Execute("Select CMNME FROM RCM")
			If Not RecRCM.EOF Then
				txtCMNME.Text = RecRCM.Fields("CMNME").Value

			Else
				txtCMNME.Text = ""
			End If
			RecRCM.Close()
		End Sub

		Private Sub cboARMDC_Change(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cboARMDC.Change
			txttghdon.Text = Find("RAR", "ARORA", " WHERE ARDOC='" & cboARMDC.defText & "'")
		End Sub

		Private Sub cboARMDC_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboARMDC.ClickEvent
			Dim sql As Object
			sql = "SELECT ARDOC FROM RAR WHERE ARTYP='I' AND CMCNO='" & cboARCNO.defText & "'"
			AdoINVOICE.ConnectionString = ConnectString
			AdoINVOICE.ConnectionTimeout = 10
			AdoINVOICE.CommandType = ADODB.CommandTypeEnum.adCmdText
			AdoINVOICE.RecordSource = sql
			AdoINVOICE.Refresh()
			txttghdon.Text = Find("RAR", "ARORA", " WHERE ARDOC='" & cboARMDC.defText & "'")
		End Sub

		Private Sub cmdAddnew_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddnew.Click
			NewTrans = True
			ISEDITING = True
			ISADDING = True
			WriteToScreen()
			ButtonStatus()
		End Sub

		Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
			NewTrans = False
			ISADDING = False
			ISEDITING = False
			ButtonStatus()
		End Sub

		Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click
			ISEDITING = True
			'ISADDING = True
			WriteToScreen()
			ButtonStatus()

		End Sub

		Private Sub cmdexit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdexit.Click
			Me.Close()
		End Sub

		Private Sub cmdNewItem_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdNewItem.Click
			ISEDITING = True
			ISADDING = True
			WriteToScreen()
			ButtonStatus()
		End Sub

		Private Sub cmdReview_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReview.Click
			Dim myform As New frmReview
			'myform.Tag = "PUR500"
			'myform.Show()
			frmReview.Tag = "RAR500"
			frmReview.ShowDialog()
			ISEDITING = False
			ISADDING = False
			Dim tag As String = frmReview.Tag
			If tag <> "" Then
				txtARDOC.Text = frmReview.GridList.Columns(1).Text
				cboARMDC.defText = frmReview.GridList.Columns(2).Text
				frmReview.Close()
				'If myform.Tag <> "" Then
				'txtARDOC.Text = myform.GridList.Columns(1).Text
				'cboARMDC.defText = myform.GridList.Columns(2).Text
				'myform.Close()
				'txtBLLNE = 1
				WriteToScreen()
			End If
		End Sub

		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click
			WriteToFile()
			ISADDING = False
			ISEDITING = False
			NewTrans = False
			ButtonStatus()
		End Sub


		Private Sub frmRAR500_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
			OpenFile()
			VB6_AddADODataBinding()
			'cboARMDC.RowSource = ADOBind_AdoINVOICE.DataSource
			cboARCNO.RowSource = ADOBind_AdoRCM.DataSource
			cboARCCD.RowSource = ADOBind_AdoCCM.DataSource
			Dim myDataCaptions As String() = {"Doc NO.", "MDOC"}
			Dim myDataFields As String() = {"ARDOC", "ARMDC"}
			setDataGridView(myDataCaptions, myDataFields, Me.GrdRAR)
		End Sub

		'Private Sub txtARBAM_KeyPress(KeyAscii As Integer)
		'If KeyAscii = 13 Then
		'    If KeyAscii = 13 Then
		'        txtARAMT = Val(txtARBAM) * Val(txtARCER)
		'        txtARAMT.SetFocus
		'        CalculateRAR
		'    End If
		'End If
		'End Sub

		'Private Sub txtARCNO_KeyPress(KeyAscii As Integer)
		'Dim Result As String
		'If KeyAscii = 13 Then
		'    Result = Find("RCM", "CMNME", "WHERE CMCNO='" & txtARCNO & "'")
		'    If Result = "" Then
		'        cboARCNO.Visible = True
		'        cboARCNO.SetFocus
		'    Else
		'        cboARCNO.Visible = False
		'        txtCMNME = Result
		'        txtARCCD.SetFocus
		'        AdoRAR.Refresh
		'    End If
		'End If
		'End Sub
		Sub OpenFile()
			Dim sql As Object
			AdoRCM.ConnectionString = ConnectString
			AdoRCM.ConnectionTimeout = 10
			AdoRCM.CommandType = ADODB.CommandTypeEnum.adCmdText
			AdoRCM.RecordSource = "Select RCM.* from RCM"
			AdoRCM.Refresh()

			AdoRAR.ConnectionString = ConnectString
			AdoRAR.ConnectionTimeout = 10
			AdoRAR.CommandType = ADODB.CommandTypeEnum.adCmdText
			AdoRAR.RecordSource = "SELECT RAR.* From RAR WHERE (ARDOC='" & txtARDOC.Text & "')"
			AdoRAR.Refresh()

			AdoCCM.ConnectionString = ConnectString
			AdoCCM.ConnectionTimeout = 10
			AdoCCM.CommandType = ADODB.CommandTypeEnum.adCmdText
			sql = "Select * from CCM"
			AdoCCM.RecordSource = sql
			AdoCCM.Refresh()

			AdoRARLine.ConnectionString = ConnectString
			AdoRARLine.ConnectionTimeout = 10
			AdoRARLine.CommandType = ADODB.CommandTypeEnum.adCmdText
			AdoRARLine.RecordSource = "SELECT RAR.* From RAR WHERE (ARDOC <> ARMDC)" & " and (ARDOC='" & txtARDOC.Text & "') ORDER BY ARMDC"
			AdoRARLine.Refresh()

			AdoRARINV.ConnectionString = ConnectString
			AdoRARINV.ConnectionTimeout = 10
			AdoRARINV.CommandType = ADODB.CommandTypeEnum.adCmdText
			AdoRARINV.RecordSource = "SELECT RAR.ARDOC From RAR WHERE (ARTYP='I')" & " and (CMCNO='" & cboARCNO.defText & "') ORDER BY ARDOC"
			AdoRARINV.Refresh()

		End Sub
		Sub WriteToFile()
			Dim sql As Object
			If IsDate(txtARDTE.Text) Then txtARDTE.Text = VB6.Format(txtARDTE.Text, "yyyy-mm-dd hh:mm:ss")

			If ISADDING Then
				'txtARDOC = TakeOrderNumberACN("CNNPN", True)
				sql = "Insert Into RAR (ARDOC,ARDTE,ARTYP,CMCNO,ARCCD,ARCER" & ",ARMDC,ARAMT,ARORA,ARLUS,ARRMK,ARPAY) values " & "('" & txtARDOC.Text & "','" & txtARDTE.Text & "','P','" & cboARCNO.defText & "','" & cboARCCD.defText & "','" & txtARCER.Text & "','" & cboARMDC.defText & "','" & txtARAMT.Text & "','" & txtARORA.Text & "','" & UserLogin & "','" & "manual postting" & "','" & txtARPAY.Text & "')"
			Else
				sql = "UPDATE RAR SET ARDOC='" & txtARDOC.Text & "', ARMDC='" & cboARMDC.defText & "',ARDTE='" & txtARDTE.Text & "',CMCNO='" & cboARCNO.defText & "',ARCCD='" & cboARCCD.defText & "',ARCER='" & txtARCER.Text & "',ARAMT='" & txtARAMT.Text & "',ARORA='" & txtARORA.Text & "',ARLUS='" & UserLogin & "',ARPAY='" & txtARPAY.Text & "' where  ARDOC='" & txtARDOC.Text & "' and ARMDC='" & oldARMDC & "'"
			End If
			'MsgBox sql
			DBconn.Execute(sql)
			'Refresh lai du lieu
			oldARDOC = txtARDOC.Text
			oldARMDC = cboARMDC.defText
			AdoRARLine.RecordSource = "Select RAR.* from RAR " & "  WHERE ARDOC='" & txtARDOC.Text & "'"
			AdoRARLine.Refresh()
		End Sub
		Sub WriteToScreen()
			If NewTrans Then
				txtARDOC.Text = ""
				txtARDTE.Text = CStr(WorkingDate)
				cboARCNO.defText = ""
				txtCMNME.Text = ""
				cboARCCD.defText = ""
				txtARCER.Text = ""
				txtARPAY.Text = CStr(0)
			End If
			'cboARMDC.Visible = False
			Dim RecRAR As ADODB.Recordset
			If ISADDING Then
				cboARMDC.defText = ""
				txtARPAY.Text = CStr(0)
				txttghdon.Text = CStr(0)
			Else 'review
				RecRAR = DBconn.Execute("Select * from RAR where ARDOC='" & txtARDOC.Text & "'")
				With RecRAR
					txtARDOC.Text = .Fields("ARDOC").Value
					oldARDOC = txtARDOC.Text
					txtARDTE.Text = .Fields("ARDTE").Value
					cboARCNO.defText = .Fields("CMCNO").Value
					txtCMNME.Text = Find("RCM", "CMNME", " WHERE CMCNO='" & cboARCNO.defText & "'")
					cboARCCD.defText = .Fields("ARCCD").Value
					txtCMDSC.Text = Find("CCM", "CMDSC", " WHERE CMCCD='" & cboARCCD.defText & "'")
					txtARCER.Text = .Fields("ARCER").Value
					cboARCCD.defText = .Fields("ARCCD").Value
					cboARMDC.defText = .Fields("ARMDC").Value
					oldARMDC = cboARMDC.defText
					txttghdon.Text = Find("RAR", "ARORA", " WHERE ARDOC='" & cboARMDC.defText & "' AND ARTYP='I'")
					txtARAMT.Text = .Fields("ARAMT").Value
					txtARORA.Text = .Fields("ARAMT").Value
					txtARPAY.Text = IIf(IsDBNull(.Fields("ARPAY").Value), 0, .Fields("ARPAY").Value)
				End With
			End If
			If AdoRARLine.EOF Then
				Me.AdoRARLine = New Microsoft.VisualBasic.Compatibility.VB6.ADODC
				AdoRARLine.ConnectionString = ConnectString
				AdoRARLine.ConnectionTimeout = 10
				AdoRARLine.CommandType = ADODB.CommandTypeEnum.adCmdText
			End If
			AdoRARLine.RecordSource = "Select RAR.* from RAR " & "  WHERE  ARdoc='" & txtARDOC.Text & "'"
			AdoRARLine.Refresh()
		End Sub

		Sub ButtonStatus()
			cmdAddNew.Enabled = Not ISEDITING
			cmdReview.Enabled = Not ISEDITING
			cmdNewItem.Enabled = Not ISEDITING
			cmdEdit.Enabled = Not ISEDITING
			cmdSave.Enabled = ISEDITING
			cmdCancel.Enabled = ISEDITING
			FrameRAR.Enabled = NewTrans Or ISEDITING
			FrameLine.Enabled = ISEDITING
			FrameRARLine.Enabled = ISEDITING
		End Sub


		'Sub CalculateRAR()
		'Dim recRARTotal, recRARPayment As Recordset, Payment, PaymentBase
		'Set recRARTotal = DBconn.Execute("Select * from RAR where ARDOC='" & txtARDOC & "'")
		'
		'If Not recRARTotal.EOF Then
		'    txtARBAM1 = recRARTotal!ARBAM
		'    txtARAMT1 = recRARTotal!ARAMT
		'End If
		'Set recRARPayment = DBconn.Execute("Select * from RAR where ARMDC='" & txtARDOC & _
		''"' AND ARMDC<>ARDOC AND ARDOC<" & txtARDOC)



		Private Sub txtARAMT_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtARAMT.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				txtARORA.Text = CStr(CDbl(txtARAMT.Text) * CDbl(txtARCER.Text))
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtARAMT_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtARAMT.Leave
			txtARORA.Text = CStr(CDbl(txtARAMT.Text) * CDbl(txtARCER.Text))
		End Sub

		'Payment = 0
		'PaymentBase = 0
		'With recRARPayment
		'    Do While Not .EOF
		'        Payment = Payment + !ARAMT
		'        PaymentBase = PaymentBase + !ARBAM
		'        .MoveNext
		'    Loop
		'    .Close
		'End With
		'
		'txtARBBL = Val(txtARBAM1) - PaymentBase - Val(txtARBAM)
		'txtARBAL = Val(txtARAMT1) - Payment - Val(txtARAMT)
		'End Sub
		Private Sub txtARCER_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtARCER.TextChanged
			On Error Resume Next
			txtARORA.Text = CStr(CDbl(txtARCER.Text) * CDbl(txtARAMT.Text))
		End Sub

		Private Sub txtARCER_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtARCER.Leave
			On Error Resume Next
			txtARORA.Text = CStr(CDbl(txtARCER.Text) * CDbl(txtARAMT.Text))
		End Sub
		Private Sub txtARDOC_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtARDOC.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				txtARDOC.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
		Private Sub txtARDTE_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtARDTE.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				If Not IsDate(txtARDTE.Text) Then
					MsgBox("Date invalid")
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
	End Class
End Namespace